﻿using System.ComponentModel;
using Portal.FileSystemData;
using System.Collections.Generic;


namespace Portal.Modules.ImageBrowser
{
  /// <summary>
  /// Summary description for FileDataBusinessObj
  /// </summary>
  [DataObject]
  public class FileDataBusinessObj
  {
    /// <summary>
    /// Get all the subdirectories.
    /// </summary>
    /// <param name="dataAccessor">The helper object to access the data</param>
    /// <param name="relativePath">The relative virtual path of the directory</param>
    /// <returns>The subdirectories or null if not found</returns>
    [DataObjectMethod(DataObjectMethodType.Select, false)]
    public static IEnumerable<DirectoryWrapper> GetDirectoryData(DataAccessor dataAccessor, string relativePath)
    {
      var dir = GetDirectory(dataAccessor, relativePath);
      if (dir != null)
        return dir.SubDirectories;
      else
        return null;
    }

    /// <summary>
    /// Get all the files.
    /// </summary>
    /// <param name="dataAccessor">The helper object to access the data</param>
    /// <param name="relativePath">The relative virtual path of the directory</param>
    /// <returns>The files or null if not found</returns>
    [DataObjectMethod(DataObjectMethodType.Select, false)]
    public static IEnumerable<FileWrapper> GetFilesData(DataAccessor dataAccessor, string relativePath)
    {
      var dir = GetDirectory(dataAccessor, relativePath);
      if (dir != null)
        return dir.Files;
      else
        return null;
    }

    /// <summary>
    /// Get all the files with paging support.
    /// </summary>
    /// <param name="dataAccessor">The helper object to access the data</param>
    /// <param name="relativePath">The relative virtual path of the directory</param>
    /// <returns>The files or null if not found</returns>
    [DataObjectMethod(DataObjectMethodType.Select, false)]
    public static IEnumerable<FileWrapper> GetFilesData(DataAccessor dataAccessor, string relativePath, int startRowIndex, int maximumRows)
    {
      var dir = GetDirectory(dataAccessor, relativePath);
      if (dir != null)
        return (IEnumerable<FileWrapper>) dir.GetItems(startRowIndex, maximumRows, false);
      else
        return null;
    }


    /// <summary>
    /// Get the directory content.
    /// </summary>
    /// <param name="dataAccessor">The helper object to access the data</param>
    /// <param name="relativePath">The relative virtual path of the directory</param>
    /// <returns>The directories and files or null if not found</returns>
    [DataObjectMethod(DataObjectMethodType.Select, true)]
    public static IEnumerable<FileSystemWrapper> GetDirectoryContent(DataAccessor dataAccessor, string relativePath)
    {
      var dir = GetDirectory(dataAccessor, relativePath);
      if (dir != null)
        return dir.AllItems;
      else
        return null;
    }


    /// <summary>
    /// Get the directory content with paging support.
    /// </summary>
    /// <param name="dataAccessor">The helper object to access the data</param>
    /// <param name="relativePath">The relative virtual path of the directory</param>
    /// <param name="startRowIndex">The index of the first item</param>
    /// <param name="maximumRows">The maximum number of items, -1 for infinite</param>
    /// <returns>The directories and files or null if not found</returns>
    [DataObjectMethod(DataObjectMethodType.Select, true)]
    public static IEnumerable<FileSystemWrapper> GetDirectoryContent(DataAccessor dataAccessor, string relativePath, int startRowIndex, int maximumRows)
    {
      var dir = GetDirectory(dataAccessor, relativePath);
      if (dir != null)
        return dir.GetItems(startRowIndex, maximumRows, true);
      else
        return null;
    }


    [DataObjectMethod(DataObjectMethodType.Select, true)]
    private static DirectoryWrapper GetDirectory(DataAccessor dataAccessor, string relativePath)
    {
      var directory = dataAccessor.RootDirectory;
      if (directory != null)
      {
        if (!string.IsNullOrEmpty(relativePath))
        {
          directory = directory.GetSubDirectory(relativePath);
        }
      }
      return directory;
    }
  }
}
